﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace WBrowser
{
    public enum enumKeyType
    {
        RedlistTitle = 1,
        BlacklistTitle = 2,
        RedlistUrl = 3,
        BlacklistUrl = 4
    }

    public enum enumLanguage
    {
        English = 1,
        VietNam = 2
    }

    public static  class GeneralClass
    {
        #region ConnectString
        public static string ConnectString()
        {
            string connectionString;
            string fileName = "mpconfig.sdf";
            string password = "";

            connectionString = string.Format(
            "DataSource=\"{0}\"; Password='{1}'", fileName, password);

            return connectionString;
        }
        #endregion

        /// <summary>
        /// Check whether Url contains key word
        /// </summary>
        /// <param name="type"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool CheckUrlKeyWord(int type, string key)
        {
            //Kiểm tra url có empty không
            if (key == string.Empty)
                return true;
            mpconfigDataSet mpconfigDataSet = new mpconfigDataSet();
            //Use linq for datatable
            var result = from keyword in mpconfigDataSet.Keywords.AsEnumerable()
                         where keyword["KeyType"].ToString() == type.ToString()
                         && key.Trim().ToLower().Contains(keyword["KeyName"].ToString())
                         select keyword;
            if (result.Count() > 0)
                return true;
            return false;
        }

        /// <summary>
        /// Check if key is keyword
        /// return true if contains, false if not contains
        /// </summary>
        /// <param name="type"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public static bool CheckTitleKeyWord(int type, string key)
        {
            //Check whether a key is empty
            if (key == string.Empty)
                return true;

            mpconfigDataSet mpconfigDataSet = new mpconfigDataSet();
            //Check whether a Key is a digit/number
            Regex r = new Regex("[0-9]+");
            if (r.Match(key).Success)
                return true;
            else
            {
                //Use linq for datatable
                var result = from keyword in mpconfigDataSet.Keywords.AsEnumerable()
                             where keyword["KeyType"].ToString() == type.ToString()
                             && keyword["KeyName"].ToString().ToLower() == key.ToLower()
                             select keyword;
                if (result.Count() > 0)
                    return true;
            }
            return false;
        }
    }
}
